#include<iostream>
#include<algorithm>
using namespace std;
int m, n;
const int maxn = 100+7;
const int maxm = 5000+7;
struct edge{int a, b, w;
  bool operator <(const edge & b) const {return w<b.w;}};
edge E[maxm];
int par[maxn], rk[maxn];
#define SELF 1
#if SELF
int find(int x) {
  if(x==par[x]) return x;
  return par[x] = find(par[x]);
}
void init() {
  for(int i=1; i<=n; ++i) {
    par[i] = i;
  }
}
void unite(int a, int b) {
  a = find(a), b = find(b);
  par[b] = a;
}
bool same(int a, int b) {
  return find(a)==find(b);
}
#else
void init()
{
	for(int i = 0;i <= n;i++){
		par[i] = i;
		rk[i] = 0;
	}
}

int find(int x)
{
	return par[x] == x ? x : par[x] = find(par[x]);
}

void unite(int x,int y)
{
	x = find(x);
	y = find(y);
	if(x == y) return ;
	if(rk[x] < rk[y])
		par[x] = y;
	else{
		par[y] = x;
		if(rk[x] == rk[y])
			rk[x]++;
	}
}

bool cmp(edge a,edge b)
{
	return a.w < b.w;
}

bool same(int x,int y)
{
	return find(x) == find(y);
}
#endif
int main() {
  while(cin>>n>>m && (n||m)) {
    for(int i=0; i<m; ++i) cin>>E[i].a>>E[i].b>>E[i].w;
    sort(E, E+m);
    int ans = 0x7f7f7f7f;
    bool has=false;
    for(int i=0; i<m; ++i) {
      init();
      unite(E[i].a, E[i].b);
      int l, r;
      l = r = E[i].w;
      int cnt = 1;
      for(int j=i+1; j<m; ++j) {
        if(!same(E[j].a, E[j].b)) {
          unite(E[j].a, E[j].b);
          cnt++;
          r = E[j].w;

        }
      }
      if(cnt==n-1) {
        ans = min(ans, r-l);
        has = true;
      }
    }
    if(has) {cout<<ans<<'\n';}
    else {cout<<"-1\n";}
  }
}
